/*
 * Copyright 2012-2013 EZC Group S.A. RCS Luxembourg B140949
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.yobidrive.y3.v1.client.http.handlers;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.yobidrive.y3.http.HttpResponseHandler;
import com.yobidrive.y3.utils.XMLUtils;
import com.yobidrive.y3.v1.model.Action;
import com.yobidrive.y3.v1.model.GetDriveDetailsResult;
import com.yobidrive.y3.v1.model.GetDriveListResult;
import com.yobidrive.y3.v1.model.GetNodeDetailsResult;
import com.yobidrive.y3.v1.model.HistoryEntry;
import com.yobidrive.y3.v1.model.Member;
import com.yobidrive.y3.v1.model.RequestChallengeResult;
import com.yobidrive.y3.v1.model.RequestTokenResult;


public class GetDriveDetailsResponseHandler extends HttpResponseHandler<GetDriveDetailsResult>{

	static private final Logger logger = Logger.getLogger(GetDriveDetailsResponseHandler.class);
	
	@Override
	public GetDriveDetailsResult handleResponse(HttpResponse response)
			throws ClientProtocolException, IOException {
		
		//Call super to handle every error messages
		super.handleResponse(response);
		
		GetDriveDetailsResult result = new GetDriveDetailsResult();
		
		//Parse XML response
		HttpEntity entity = response.getEntity();
		
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(entity.getContent());
			Element rootElement = doc.getDocumentElement(); 
			
			result.id			= XMLUtils.getChildText(rootElement, "id");
			result.name 		= XMLUtils.getChildText(rootElement, "name");
			result.language 	= XMLUtils.getChildText(rootElement, "language");
			result.mailpolicy	= XMLUtils.getChildText(rootElement, "mailpolicy");
			result.isEsafe		= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "isesafe"), false);
			result.canDelete	= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "candelete"), false);
			result.isEmpty		= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "isempty"), false);
			result.publishPolicy= XMLUtils.getChildText(rootElement, "publishpolicy");
			result.downloadPolicy= XMLUtils.getChildText(rootElement, "downloadpolicy");
			result.isPayer	= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "ispayer"), false);
			result.enableManualPublish	= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "enablemanualpublish"), false);
			result.enableAuditTrail	= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "enableaudittrail"), false);
			
			Element ownersElement = XMLUtils.getChildByName(rootElement, "owners");
			if ( ownersElement != null  ) {
				NodeList members = ownersElement.getElementsByTagName("member");
				Member[] owners = new Member[members.getLength()];
				for ( int i=0; i<members.getLength(); i++) {
					owners[i] = new Member();
					owners[i].email = XMLUtils.getChildText(members.item(i),"email");
					owners[i].status= XMLUtils.getChildText(members.item(i),"status");
				}
				result.owners = owners;
			}
			
			Element programmedElement = XMLUtils.getChildByName(rootElement, "programmed");
			if ( programmedElement != null  ) {
				NodeList members = programmedElement.getElementsByTagName("member");
				Member[] programmed = new Member[members.getLength()];
				for ( int i=0; i<members.getLength(); i++) {
					programmed[i] = new Member();
					programmed[i].email = XMLUtils.getChildText(members.item(i),"email");
				}
				result.programmed = programmed;
			}
			
			Element readyElement = XMLUtils.getChildByName(rootElement, "readytoprogram");
			if ( readyElement != null  ) {
				NodeList members = readyElement.getElementsByTagName("member");
				Member[] ready = new Member[members.getLength()];
				for ( int i=0; i<members.getLength(); i++) {
					ready[i] = new Member();
					ready[i].email = XMLUtils.getChildText(members.item(i),"email");
				}
				result.readyToProgram = ready;
			}
			
			Element pendingElement = XMLUtils.getChildByName(rootElement, "pending");
			if ( pendingElement != null  ) {
				NodeList members = pendingElement.getElementsByTagName("member");
				Member[] pending = new Member[members.getLength()];
				for ( int i=0; i<members.getLength(); i++) {
					pending[i] = new Member();
					pending[i].email = XMLUtils.getChildText(members.item(i),"email");
				}
				result.pending = pending;
			}
			
		} catch (Exception ex) {
			logger.error("Error handling response",ex);
			return null;
		}
		
		return result;
	}

	
	
}
